package xyz.xmh.pattern.behavioral.mementopattern;

/**
 * @author Administrator
 * 备忘录模式演示类
 */
public class MementoPatternDemo {

    public static void main(String[] args) {
        // 被备份的类
        Originator originator = new Originator();
        // 备忘录管理类
        CareTaker careTaker = new CareTaker();

        // 添加数据
        originator.setState("State #1");
        originator.setState("State #2");
        // 保存状态
        careTaker.addMemento(originator.saveState());
        originator.setState("State #3");
        careTaker.addMemento(originator.saveState());
        originator.setState("State #4");

        System.out.println("Current State:" + originator.getState());

        // 恢复状态
        originator.restoreState(careTaker.getMemento(0));
        System.out.println("First Saved State:" + originator.getState());

        originator.restoreState(careTaker.getMemento(1));
        System.out.println("Second Saved State:" + originator.getState());


    }
}
